import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

public class GuavaTester {
	private static Connection conn = null;
	private static PreparedStatement pstmt = null;
	private static ResultSet rs = null;
	private static Logger logger = Logger.getLogger(GuavaTester.class);

	@SuppressWarnings("unchecked")
	public static void main(String args[]) throws Exception {
		CacheLoader<String, String> cl = new CacheLoader() {
			@Override
			public Object load(Object arg0) throws Exception {
				return getFromDatabase((String) arg0);
			}
		};
		LoadingCache<String, String> stockInfoCache = CacheBuilder.newBuilder().maximumSize(100).expireAfterAccess(30, TimeUnit.MINUTES).build(cl);
		logger.debug("................first time............ ");
		System.err.println(stockInfoCache.get("010107"));
		logger.debug("................second time............ ");
		System.err.println(stockInfoCache.get("010107"));
	}

	private static String getFromDatabase(String code) throws Exception {
		BaseDAO bd = new BaseDAO();
		String[] values = { code };
		String inter_code = "";
		ResultSet rs = bd.exeuteQuery("select * from tbstockinfo where report_code = ?", values);
		while (rs.next()) {
			inter_code = rs.getString("inter_code");
		}
		return inter_code;
	}
}